﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Business.Apartments;
using System.ComponentModel;
using blackhouse.BlackApart.Web;
using System.Web.UI;
using blackhouse.JavaScript.JQuery;

namespace blackhouse.Aparts.Web.Modules {
	public class ShowReservationPopupLink : HyperLink {

		#region Fields

		private IApartmentGroup apartment;
		private int frameWidth = 600;
		private int frameHeight = 480;
		private DisplayReservationPopupMode displayMode = DisplayReservationPopupMode.ForMakeReservation;

		#endregion

		#region Properties

		[Browsable(true)]
		[Category("Display")]
		public IApartmentGroup Apartment {
			get {
				return this.apartment;
			}
			set {
				if ((this.apartment == null && value != null) || (this.apartment != null && value == null) || this.apartment.Id != value.Id) {
					this.apartment = value;
					this.RefreshLink();
				}
			}
		}

		[Browsable(true)]
		[Category("Display")]
		[DefaultValue(600)]
		public int FrameWidth {
			get { return this.frameWidth; }
			set {
				if (this.frameWidth != value) {
					this.frameWidth = value;
					this.RefreshLink();
				}
			}
		}

		[Browsable(true)]
		[Category("Display")]
		[DefaultValue(480)]
		public int FrameHeight {
			get { return this.frameHeight; }
			set {
				if (this.frameHeight != value) {
					this.frameHeight = value;
					this.RefreshLink();
				}
			}
		}


		[Browsable(true)]
		[Category("Display")]
		[DefaultValue(DisplayReservationPopupMode.ForMakeReservation)]
		public DisplayReservationPopupMode DisplayMode {
			get { return this.displayMode; }
			set {
				if (this.displayMode != value) {
					this.displayMode = value;
					this.RefreshLink();
				}
			}
		}

		#endregion

		#region Event Handlers

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			this.Attributes.Add("rel", "nofollow");
			this.Attributes.Add("popup", "reservation");
			this.AddJs();

			this.RefreshLink();
		}

		#endregion

		#region Methods

		private void RefreshLink() {
			if (this.Apartment != null || this.DisplayMode == DisplayReservationPopupMode.ForChangeParams) {

				if (this.DisplayMode == DisplayReservationPopupMode.ForMakeReservation)
					this.ToolTip = "Sprawdź termin i cenę noclegu";
				if (this.DisplayMode == DisplayReservationPopupMode.ForChangeParams)
					this.ToolTip = "Zmień termin i liczbę osób";

				if (this.Apartment != null)
					this.ToolTip += (" - " + this.Apartment.Name);

				char sign = '?';
				string url = UrlUtils.LocalizeUrl(string.Format("/pages/showreservationpopup.aspx"));
				if (Apartment != null) {
					url += (sign + "groupId=" + this.Apartment.Id);
					sign = '&';
				}
				if (this.DisplayMode != DisplayReservationPopupMode.ForMakeReservation) {
					url += (sign + "mod=" + ((int)this.DisplayMode));
					sign = '&';
				}

				SearchState state = SearchState.GetFromContext();
				if (state.IsSet) {
					url += sign + "search=" + state.ToString();
					sign = '&';
				}

				url += string.Format(sign + "iframe=true&width={0}&height={1}", this.FrameWidth, this.FrameHeight);
				this.NavigateUrl = url;
			}
			else {
				this.Visible = false;
			}
		}

		private void AddJs() {

			ClientScriptManager jsManager = Page.ClientScript;
			string scriptName = "prettyPhoto";
			string scriptSrc = "/prettyphoto/js/jquery.prettyPhoto.js";
			if (!jsManager.IsClientScriptIncludeRegistered(scriptName)) {
				jsManager.RegisterClientScriptInclude(scriptName, scriptSrc);
			}

			JQueryLibraries jql = new JQueryLibraries();
			jql.IncludeFileBasedOnJQuery("blackhouse.Aparts.Web.Scripts.reservationpopup-link.js,blackhouse.Aparts.Web");
		}

		#endregion

	}

	#region Outer

	public enum DisplayReservationPopupMode {
		ForMakeReservation,
		ForChangeParams
	}

	#endregion

}